﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Management;

namespace App2Wpf.Forms
{
    public partial class remoteWindow : Form
    {
        public remoteWindow()
        {
            ResetTheme(true);
            InitializeComponent();
            Control.CheckForIllegalCrossThreadCalls = false;  //
            this.pictureBox1.ContextMenuStrip = this.contextMenuStrip1;
            
        }

        public delegate void SendSingal(object sender, FormClosingEventArgs e);

        public event SendSingal Send;

        public delegate void ToolStripMenuItem(object sender, EventArgs e);

        public event ToolStripMenuItem menu_Click;

        bool Selected { get; set; }


        private void remoteWindow_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.pictureBox1.Dispose();
            
            Send(sender, e);
        }

        private void remoteWindow_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(new Pen(Color.Black, 1),
                this.DisplayRectangle);
            //ControlStyles.ResizeRedraw
            DoubleBuffered = true;

        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.DrawRectangle(new Pen(Color.Black, 100),
                this.DisplayRectangle);
        }

        private void remoteWindow_Resize(object sender, EventArgs e)
        {
            Invalidate();
        }

        [DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        private void ResetTheme(bool enabled)
        {
            try
            {
                if (Environment.OSVersion.Version.Major >= 6
                    //&& Environment.OSVersion.Version.Build >= 17763
                    )
                {
                    if (Environment.OSVersion.Version.Major >= 6
                    //&& Environment.OSVersion.Version.Build >= 18985
                    )
                    {
                        int useImmersiveDarkMode = enabled ? 1 : 0;
                        // darkmode 20
                        DwmSetWindowAttribute(Handle, (int)20, ref useImmersiveDarkMode, sizeof(int));
                    }
                }
            }
            catch (Exception e)
            {

                //throw;
            }

        }

        public void oPENToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menu_Click(sender, e);
            //ViewerF sessionViewer = new ViewerControl(ipAdd, wininfo);
            //sessionViewer.VirtualDeskForm.Show();
        }

        private void cLOSEToolStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void xWindowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            menu_Click(sender, e);
        }

        //private void remoteWindow_Paint_1(object sender, PaintEventArgs e)
        //{
        //    e.Graphics.DrawRectangle(new Pen(Color.Black, 1),
        //        this.DisplayRectangle);
        //}
    }
}
